(function(window){
    function Player($audio){
        return new Player.prototype.gouzao($audio);
    }
    Player.prototype = {
        constructor:Player,
        musicList:[],
        gouzao:function($audio){
            this.$audio = $audio;
            this.audio = $audio.get(0);
        }
        ,now_index:-1,
        //播放音乐
        playMusic:function(index){
            if(this.musicList.length > 0) {
                var $this = this;
                if(this.now_index == index) {
                    //正在播放的就是这首
                    if(this.audio.paused) {
                        //恢复播放
                        this.audio.play();
                    } else {
                        //暂停播放
                        this.audio.pause();
                    }
                } else {
                    this.now_index = index;
                    //index不同，获取index的音频链接
                    this.getMusic($this.musicList[index].mid,function(url) {
                        if( url ) {
                            $this.$audio.attr('src',url);
                            $this.audio.play();
                            $this.now_index = index;
                        } else {
                            window.popup('获取歌曲失败QvQ');
                        }
                    });
                }
            }
        }, 
        //根据mid获取音频链接
        getMusic: function (mid,callback){
            var url ='https://u.y.qq.com/cgi-bin/musicu.fcg?-=getplaysongvkey13173184899026058&g_tk=5381&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq.json&needNewCode=0&data={"req":{"module":"CDN.SrfCdnDispatchServer","method":"GetCdnDispatch","param":{"guid":"6704840616","calltype":0,"userip":""}},"req_0":{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","param":{"guid":"6704840616","songmid":["' +mid +'"],"songtype":[1],"uin":"0","loginflag":1,"platform":"20"}},"comm":{"uin":0,"format":"json","ct":24,"cv":0}}';
            $.ajax({
                url: url,
                type: 'get',
                dataType: 'jsonp',
                success: function(data) {
                    if(data.req_0.data.midurlinfo[0].purl){
                        callback('http://dl.stream.qqmusic.qq.com/' + data.req_0.data.midurlinfo[0].purl);
                    }else{
                        callback(data.req_0.data.midurlinfo[0].purl);
                    }
                }
            });
        },
        //切换上一首音乐
        preMusic:function(){
            var index = this.now_index -1;
            if(index < 0){
                index = this.musicList.length;
            }
            return index;
        },
        //切换下一首音乐
        nextMusic:function(){
            var index =this.now_index +1;
            if(index >= this.musicList.length){
                index = 0;
            }
            return index;
        },
        //获取播放器的音乐时长信息，传入一个回调函数获取格式化的时间
        getMusicInfo: function(callback) {
            var duration = this.audio.duration;
            var currentTime = this.audio.currentTime;
            var data = this.formatData(currentTime, duration);
            callback(currentTime, duration, data);
        },
        //格式化时间
        formatData: function(currentTime,duration){
            var currentM = parseInt(currentTime/60);
            if(currentM<10){
                currentM = '0'+currentM;
            }
            var currentS = parseInt(currentTime%60);
            if(currentS<10){
                currentS = '0'+currentS;
            }
            var durationM = parseInt(duration/60);
            var durationS = parseInt(duration%60);
            if(durationM<10){
                durationM = '0'+durationM;
            }
            if(durationS<10){
                durationS = '0'+durationS;
            }
            return {currentTime:currentM+':'+currentS,duration:durationM+':'+durationS};
        },
        //设置播放时间
        setCurrentTime: function(time){
            this.audio.currentTime = time;
        },
        //播放器播放位置改变监听
        musicUpdate:function(callback){
            this.$audio.on("timeupdate",function(){
                callback();
            })
        },
        //播放器播放监听
        play:function(callback){
            //播放器播放监听
            this.$audio.on('play',function(){
                callback();
            });
        },
        //播放器暂停监听
        pause:function(callback){
            this.$audio.on('pause',function(){
                callback();
            })
        },
        //当播放器加载完元数据的事件：时长，文字。。。
        loadedMetaData:function(callback){
            this.$audio.on('loadedmetadata',function(){
                callback();
            })
        },
        
        
    }
    Player.prototype.gouzao.prototype = Player.prototype;
    window.Player = Player;
})(window)